package com.cocoiko;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.cocoiko.client.ClientUtils;
import com.cocoiko.customview.CocoikoListAdapter;
import com.cocoiko.customview.CocoikoListView;
import com.cocoiko.customview.GridAdapter;
import com.cocoiko.info.CocoikoInfo;
import com.cocoiko.utils.CocoikoDialog;
import com.cocoiko.utils.CocoikoEvent;
import com.cocoiko.utils.CocoikoEventPostData;

public class Home extends Activity implements OnClickListener {

	private static final int COCOIKO_INPUT = 1;
	ImageView avatar;
	TextView comment;
	LinearLayout tool_button;
	LinearLayout shop_info;
	LinearLayout lead_info;
	LinearLayout right;

	CocoikoListView listkokoiko;
	GridView lead_avatar;

	CocoikoListAdapter kokoikos;
	ArrayList<CocoikoInfo> kokoiko_infos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);

		avatar = (ImageView) findViewById(R.id.avatar);
		comment = (TextView) findViewById(R.id.comment);
		tool_button = (LinearLayout) findViewById(R.id.tool_button);
		shop_info = (LinearLayout) findViewById(R.id.shop_info);
		lead_info = (LinearLayout) findViewById(R.id.lead_info);
		right = (LinearLayout) findViewById(R.id.right);

		lead_avatar = (GridView) findViewById(R.id.lead_avatar);
		listkokoiko = (CocoikoListView) findViewById(R.id.listfriend);

		avatar.setOnClickListener(this);
		comment.setOnClickListener(this);
		tool_button.setOnClickListener(this);
		shop_info.setOnClickListener(this);
		right.setOnClickListener(this);

		
		// get event
		
		String message = ClientUtils.cocoikoTookme(this);
		if (message != null) {
			CocoikoDialog.MessageDialog(this, message);
		}
		
		
		// insert image to gridview
		ArrayList<Bitmap> icons = new ArrayList<Bitmap>();
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		icons.add(bitmap);
		icons.add(bitmap);
		icons.add(bitmap);
		lead_avatar.setAdapter(new GridAdapter(this, icons));

		// test list kokoiko
		kokoikoOfFriendList();

	}

	/*
	 * 
	 */
	public void kokoikoOfFriendList() {

		// get cocoiko info
		getKokoikoInfo();
		listkokoiko.addDataIntoList(kokoiko_infos);

	}

	/*
	 * 
	 */
	public void getKokoikoInfo() {
		kokoiko_infos = new ArrayList<CocoikoInfo>();

		// test
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.icon);
		ArrayList<Bitmap> lead_avatar = new ArrayList<Bitmap>();
		lead_avatar.add(bitmap);
		lead_avatar.add(bitmap);

		CocoikoInfo info = new CocoikoInfo(bitmap, "comment", bitmap,
				"Shop name", "shop address", lead_avatar, "2011/11/1");
		kokoiko_infos.add(info);
		info = new CocoikoInfo(bitmap, "comment", null, "Shop name",
				"shop address", null, "2011/11/1");
		kokoiko_infos.add(info);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == avatar) {
			Log.i("ok", "ok");
		}
		if (v == comment) {

			Intent intent = new Intent(this, CocoikoInput.class);
			startActivityForResult(intent, COCOIKO_INPUT);
		}
	}

	// �s�������X���Ȃ��ꍇ
	public void noEvent() {
		LayoutParams params = (LayoutParams) tool_button.getLayoutParams();
		params.height = 0;
		tool_button.setLayoutParams(params);
		params = (LayoutParams) shop_info.getLayoutParams();
		params.height = 0;
		shop_info.setLayoutParams(params);
	}

	// �s�������X������ꍇ
	public void existEvent() {
		LayoutParams params = (LayoutParams) tool_button.getLayoutParams();
		params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
		tool_button.setLayoutParams(params);

		params = (LayoutParams) shop_info.getLayoutParams();
		params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
		tool_button.setLayoutParams(params);
	}

	// ��Ă���l�����Ȃ�
	public void noLeadUser() {
		LayoutParams params = (LayoutParams) lead_info.getLayoutParams();
		params.height = 0;
		lead_info.setLayoutParams(params);
	}

	// ��Ă���l������
	public void existLeadUser() {
		LayoutParams params = (LayoutParams) lead_info.getLayoutParams();
		params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
		lead_info.setLayoutParams(params);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			if (requestCode == COCOIKO_INPUT) {
				// cocoiko input
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (item.getItemId()) {
		case R.id.home:
			if (!this.getClass().getName().equals(Home.class.getName())) {
				finish();
				intent = new Intent(this, Home.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.friends:
			if (!this.getClass().getName().equals(Friends.class.getName())) {
				finish();
				intent = new Intent(this, Friends.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.map:
			if (!this.getClass().getName().equals(Map.class.getName())) {
				finish();
				intent = new Intent(this, Map.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.allkokoiko:
			if (!this.getClass().getName().equals(AllCocoiko.class.getName())) {
				finish();
				intent = new Intent(this, AllCocoiko.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.message:
			if (!this.getClass().getName().equals(Message.class.getName())) {
				finish();
				intent = new Intent(this, Message.class);
				startActivity(intent);
				finish();
			}
			break;
		case R.id.setting:
			if (!this.getClass().getName().equals(Setting.class.getName())) {
				finish();
				intent = new Intent(this, Setting.class);
				startActivity(intent);
				finish();
			}
			break;
		}

		return true;
	}

}
